rm(list = ls())
here::i_am(file.path("code", "11_naacp_membership_figs.R"))
library(here)
source(here("code", "config.R"))

library(sf)
library(readxl)

df <- read_excel(here("data", "naacp", "naacp_national_membership.xlsx"))

p <- bind_rows(
  df,
  tibble(year = 1909, members = 0)
) |>
  filter(!is.na(members)) |>
  arrange(year) |>
  filter(year <= 1950) |>
  ggplot(aes(x = year, y = members/1000)) +
  geom_point() +
  geom_line() +
  
  my_theme() +
  labs(
    x = "Year",
    y = "NAACP membership (1000s)"
  )

p_color <- p +
  geom_vline(xintercept = 1917, linetype = "dashed", color = "#F8766D") +
  geom_label(label = "WWI", x =  1917, y = 150, color = "#F8766D") +
  geom_vline(xintercept = 1941, linetype = "dashed", color = "#F8766D") +
  geom_label(label = "WWII", x =  1941, y = 250, color = "#F8766D")
ggsave(file.path(fig_dir, "color", "naacp_membership.pdf"), plot = p_color, width = 5, height = 3)

p_bw <- p + 
  geom_vline(xintercept = 1917, linetype = "dashed") +
  geom_label(label = "WWI", x =  1917, y = 150,) +
  geom_vline(xintercept = 1941, linetype = "dashed") +
  geom_label(label = "WWII", x =  1941, y = 250)
ggsave(file.path(fig_dir, "bw", "naacp_membership.pdf"), plot = p_bw, width = 5, height = 3)

################################################################################
# NAACP map

naacp_cities <- read_csv(
  here("data", "xw", "naacp_cities.csv")
) |>
  mutate(
    city = ifelse(branch_id == "northern-ca", "Northern CA", city)
  )

states <- st_read(here("data", "shp", "cb_2018_us_state_20m", "cb_2018_us_state_20m.shp")) |>
  filter(!STUSPS %in% c("PR", "HI", "AK"))

plt_df <- naacp_cities |>
  filter(!is.na(lon)) |>
  group_by(branch_id, lon, lat) |>
  summarize(
    n = sum(n)
  ) |>
  filter(n >= 100) |>
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

p <- ggplot(states) +
  geom_sf(fill = "#dddddd", alpha = .8, color = "#ffffff", linewidth = .5) +
  theme_void(base_size = fig_base_size) +
  theme(
    legend.position = "top",
    legend.box = "vertical"
  )

p_color <- p +
  geom_sf(data = plt_df, size = 3, color = "#F8766D", alpha = .9) +
  coord_sf(crs = st_crs(5070))
ggsave(file.path(fig_dir, "color", "naacp_map.pdf"), plot = p_color, width = 6, height = 4)

p_bw <- p +
  geom_sf(data = plt_df, size = 3, alpha = .9) +
  coord_sf(crs = st_crs(5070))
ggsave(file.path(fig_dir, "bw", "naacp_map.pdf"), plot = p_bw, width = 6, height = 4)

greg <- read_excel(here("data", "naacp", "Gregory_NAACP combined 6e_DoNotCirculate.xlsx"))

greg_plt <- greg |>
  rename(lat = Latitude, lon = Longitude, n = `Members level`) |>
  filter(database == "branch", Year < 1941, !is.na(lon), !is.na(lat)) |>
  filter(n != "unknown", n != "1-49", n != "50-99") |>
  distinct(lat, lon) |>
  mutate(lat = as.numeric(lat), lon = as.numeric(lon)) |>
  filter(!is.na(lon), !is.na(lat)) |>
  filter(lon < 0, lat > 10) |>
  st_as_sf(coords = c("lon", "lat"), crs = 4326)
  
p <- ggplot(states) +
  geom_sf(fill = "#dddddd", alpha = .8, color = "#ffffff", linewidth = .5) +
  theme_void(base_size = fig_base_size)

p_color <- p +
  geom_sf(data = greg_plt, size = 3, color = "#00BFC4", alpha = .9) +
  coord_sf(crs = st_crs(5070))
ggsave(file.path(fig_dir, "color", "naacp_map_gregory.pdf"), plot = p_color, width = 6, height = 4)

p_bw <- p +
  geom_sf(data = greg_plt, size = 3, alpha = .9) +
  coord_sf(crs = st_crs(5070))
ggsave(file.path(fig_dir, "bw", "naacp_map_gregory.pdf"), plot = p_bw, width = 6, height = 4)
